Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Python -Dictionaries
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Python -Sets
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Python Functions and Modules
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Recursive Functions

Recursive functions are functions that call themselves in their definition, enabling the function to repeat its behavior until a base condition (also known as a stopping condition) is satisfied. This technique is particularly useful for solving problems that can be broken down into smaller, similar subproblems.

Recursive functions leverage the divide and conquer strategy where a problem is divided into smaller chunks, each chunk is solved individually, and the individual results are combined to form the final result. This makes recursive approaches particularly elegant for problems such as factorial calculation, Fibonacci sequence generation, and more.

Key Points to consider

  • Base Case: Every recursive function must have a base case, which is a condition under which the function returns a value without making further recursive calls.
  • Recursive Case: The part of the function where the recursive call is made. Here, the problem is broken down into smaller instances.
  • Stack Overflow Risk: If the recursion is too deep, it might lead to a stack overflow error, hence it's crucial to ensure that each recursive call progresses towards the base case.

Defining a Recursive Function

A classic example of a recursive function is one that calculates the factorial of a number. The factorial of a number ( n ) (denoted as ( n! )) is the product of all positive integers less than or equal to ( n ).

Example

Creating a recursive function to calculate the factorial of a number.

Python3

. . . .

Explanation:

  • def factorial(n): defines the function with one parameter ( n ).
  • The function first checks if ( n ) is equal to 1. If true, it returns 1 (because the factorial of 1 is 1).
  • If not, the function calls itself but with ( n-1 ) as the argument. This recursive call continues until ( n ) becomes 1.
  • The recursive calls multiply the numbers as they return back up the stack (i.e., 5 * 4 * 3 * 2 * 1).

Recursive functions are also adept at handling more complex data structures and algorithms, such as navigating file systems, parsing nested data structures, or implementing algorithmic strategies like divide and conquer in sorting algorithms.

Example

Implementing a recursive function to compute the Fibonacci sequence, where each number is the sum of the two preceding ones.

Python3

. . . .

Explanation:

  • def fibonacci(n): defines the Fibonacci function.
  • It checks if ( n ) is less than or equal to 1. If true, it returns ( n ) (since the first two Fibonacci numbers are 0 and 1).
  • Otherwise, the function recursively calls itself to get the sum of the two preceding Fibonacci numbers. This recurs until reaching the base cases (0 or 1).

Recursive functions illustrate a powerful approach to solving problems by breaking them into smaller chunks and using the function itself to solve those chunks. However, it's essential to manage the depth of recursion to avoid performance issues and ensure that each recursive call brings the computation closer to the base case.

Python - Function Arguments
Python - Variable scope
Mark as Completed

On this page

Key Points to consider

Defining a Recursive Function

Example

Example